package com.keeson.android_cli.util;

import android.content.Context;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;


/**
 * Created by wendy.song on 2017/1/11.
 */

public class CommonUtils {

    /**
     * 核对手机号正确
     *
     * @param phone
     * @return
     */
    public static boolean checkPhone(String phone) {
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[0|1|2|3|4|5|6|7|8|9]))\\d{8}$";
        if (phone.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            return m.matches();
        }
    }

    /**
     * 核对手机号正确
     * 台湾：09开头后面跟8位数字
     * 香港：9或6开头后面跟7位数字
     * 澳门：66或68开头后面跟6位数字
     *
     * @param phone
     * @return
     */
    public static boolean checkPhone2(Context context, String phone) {
        return isCPhoneLegal(phone) || isHKPhoneLegal(phone) || isMCPhoneLegal(phone);
    }

    public static boolean checkPhone(String phone, String local) {
        if (local.equals("852"))
            return isHKPhoneLegal(phone);
        else if (local.equals("853"))
            return isMCPhoneLegal(phone);
        else
            return isCPhoneLegal(phone);
    }

    /**
     * +86
     * 中国手机号码11位数，
     * 3/9 190|2|6|7增加  预计在2.5.5更新
     * 7/2 更新
     */
    public static boolean isCPhoneLegal(String str) throws PatternSyntaxException {
        String regExp = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[0|1|2|3|4|5|6|7|8|9]))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 香港或澳门
     *
     * @param str
     * @return
     * @throws PatternSyntaxException
     */
    public static boolean isHMPhoneLegal(String str) throws PatternSyntaxException {
        return isHKPhoneLegal(str) || isMCPhoneLegal(str);
    }

    /**
     * +852
     * 香港手机号码8位数，5|6|8|9开头+7位任意数
     */
    public static boolean isHKPhoneLegal(String str) throws PatternSyntaxException {
        //        String regExp = "^(5|6|8|9)\\d{7}$";
        String regExp = "^(5|6|9)\\d{7}$";
        String regExp2 = "^(852[5|6|9])\\d{7}$";
        Pattern p1 = Pattern.compile(regExp);
        Matcher m1 = p1.matcher(str);
        Pattern p2 = Pattern.compile(regExp2);
        Matcher m2 = p2.matcher(str);
        return m1.matches() || m2.matches();
    }

    /**
     * +853
     * 澳门手机号码8位数，6开头+7位任意数
     */
    public static boolean isMCPhoneLegal(String str) throws PatternSyntaxException {
        // ^ 匹配输入字符串开始的位置
        // \d 匹配一个或多个数字，其中 \ 要转义，所以是 \\d
        // $ 匹配输入字符串结尾的位置
        String regExp = "^(6)\\d{7}$";
        String regExp2 = "^(8536)\\d{7}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        Pattern p2 = Pattern.compile(regExp2);
        Matcher m2 = p2.matcher(str);
        return m.matches() || m2.matches();
    }
}
